package com.zzyl.controller;

import cn.hutool.core.lang.tree.Tree;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.RecursionService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

@Api
@RequestMapping("/resource")
@RestController
public class RecursionController {
    @Autowired
    private RecursionService recursionService;

    @ApiOperation("查询资源列表")
    @PostMapping("/list")
    public ResponseResult<List<ResourceVo>> List(@RequestBody ResourceDto resourceDto){
        List<ResourceVo> list = recursionService.list(resourceDto);
        return success(list);
    }

    @ApiOperation("查询资源树形")
    @PostMapping("/tree")
    public ResponseResult<TreeVo> tree(@RequestBody ResourceDto resourceDto){
        TreeVo treeVo = recursionService.tree(resourceDto);
        return success(treeVo);
    }

    @ApiOperation("添加资源")
    @PutMapping()
    public ResponseResult insertResource(@RequestBody ResourceDto resourceDto){
        recursionService.insert(resourceDto);
        return success();
    }


    @PatchMapping
    @ApiOperation(value = "资源修改", notes = "资源修改")
    public ResponseResult<Boolean> updateResource(@RequestBody ResourceDto resourceDto) {
        recursionService.updateResource(resourceDto);
        return success();
    }


    @ApiOperation(value = "启用禁用",notes = "启用禁用")
    @PostMapping("/enable")
    public ResponseResult<Boolean> isEnable(@RequestBody ResourceVo resourceVo){
        recursionService.setEnable(resourceVo);
        return ResponseResult.success();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{resourceNo}")
    public ResponseResult deleteResource(@PathVariable("resourceNo") String resourceNo) {
        recursionService.deleteByResourceNo(resourceNo);
        return ResponseResult.success();
    }
}
